import React, { useEffect } from 'react'
import { Button, Popconfirm, Space, Table, message } from 'antd'
import type { PopconfirmProps, TableProps } from 'antd'
import { downDcoosApi, downshuzhiApi } from '@/service/common'

type Props = {
  data?: any
  mutate?: any
  setActiveTab?: any
}

const DcoosPage: React.FC<Props> = ({ setActiveTab, data, mutate }) => {
  useEffect(() => {
    mutate()
  }, [])
  const confirm: PopconfirmProps['onConfirm'] = (rd: any) => {
    console.log(rd, 123)
    let urlId = rd.url.split('/')
    if (rd.source === "agentplatform" || rd.source === undefined) {
      downDcoosApi(`http://127.0.0.1:5001/interface/api/delete-api?api_id=${rd.id}`).then(() => {
        message.success('下线成功')
        mutate()
      })
    } else {
      downshuzhiApi({ body: { deployTaskId: urlId[urlId.length - 1], status: "0" } }).then(() => {
        message.success('下线成功')
        mutate()
      })
    }
  }

  const cancel: PopconfirmProps['onCancel'] = (e) => {
    message.error('已取消')
  }

  const columns: TableProps<any>['columns'] = [
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      render: text => <a>{text}</a>,
    },
    {
      title: '所属项目空间',
      dataIndex: 'tenant_name',
      key: 'tenant_name',
    },
    {
      title: '接口ID',
      dataIndex: 'id',
      key: 'id',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      render(text) {
        return (
          text.split(' ')[0]
        )
      },
    },
    {
      title: '操作',
      key: 'action',
      render: (_, rd) => (
        <Space size="middle">
          <Popconfirm
            title="下线"
            description="暂不支持二次上线，下线后无法通过接口调用该能力"
            onConfirm={() => confirm(rd)}
            onCancel={cancel}
            okText="确认"
            cancelText="取消"
          >
            <Button danger size='small'>下线</Button>
          </Popconfirm>

        </Space>
      ),
    },
  ]

  return (
    <>
      <div className='mt-[24px]'>
        <div className='text-[#1C2748] text-[20px] mb-[16px]'>能力发布</div>
        <div className='text-[#1C2748] text-[14px] mb-[16px]'>发布大模型前应保证模型已完成部署</div>
        <div className='flex flex-col bg-[#fff] rounded-[8px] px-[24px]' style={{ height: 'calc(100vh - 158px)' }}>
          <div className='pt-[33px] pb-[22px]'>
            <Space>
              <Button type='primary' onClick={() => setActiveTab('dcoos-sign')}>API注册</Button>
            </Space>
          </div>
          <div className='flex-1 w-[100%] overflow-hidden'>
            {data ? <Table columns={columns} dataSource={data.data} pagination={{ defaultCurrent: 1, total: data.page.totalCount, pageSize: 5 }} /> : null}
          </div>
        </div>
      </div>
    </>

  )
}

export default DcoosPage
